package com.likianta.anykey;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.Realm;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    
    @BindView(R.id.view_pager)
    ViewPager viewPager; // 控制分页逻辑的容器
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ButterKnife.bind(this);
        
        // 初始化加载分页碎片
        initPages();
        
        // 测试...
        test();
        
    }
    
    public void initPages() {
        // 构造适配器
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Page1());
        fragments.add(new Page2());
        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);
        // 设定适配器
        viewPager.setAdapter(adapter);
        
        // 设置viewPager的初始界面为第一个界面
        viewPager.setCurrentItem(0);
        // 添加切换界面的监听器
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());
        
        // 为左分页加载卡片列表
        //PageRender();
    }
    
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_test: // 仅供测试...
            default:
        }
        
    }
    
    // 判断从GroupActivity来的返回值
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    //groupName = intent.getStringExtra("get groupName");
                    //LogUtil.d("ma you get the returned result (groupName): " + groupName);
                }
                break;
            default:
        }
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        
        // Save all data.
        //new SavedToMySharedPrefs(MainActivity.this, "card_data").setCardData(cardDataList);
    }
    
    // 页面滚动监听器功能，实现标签页左右滑动切换效果
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        
        @Override
        public void onPageSelected(int index) {
            switch (index) {
                case 0:
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0); // 强制隐藏软键盘
                    break;
                case 1:
                    break;
            }
        }
        
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
        
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
    
    public class FragAdapter extends FragmentPagerAdapter {
        
        private List<Fragment> fragments;
        
        public FragAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            // TODO Auto-generated constructor stub
            this.fragments = fragments;
        }
        
        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return fragments.get(position);
        }
        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return fragments.size();
        }
    }
    
    public void test(){
        Button button = (Button) findViewById(R.id.btn_test);
        button.setOnClickListener(this);
    
        Realm realm = Realm.getDefaultInstance();
    
        realm.beginTransaction();
        Group group = realm.createObject(Group.class); // Create a new object
        group.setGroupId(1);
        group.setIconId(R.drawable.shape_rnd_1);
        group.setGroupName(this.getResources().getString(R.string.uncata));
        realm.commitTransaction();
    
        
    
        
    }
    
}